package org.beynet.docs.http;

import java.io.IOException;
import java.io.OutputStream;
import org.apache.http.entity.ContentProducer;

/**
 * send a byte array
 * @author beynet
 *
 */
public class BandwithSaverContentProducer implements ContentProducer {
    
    public BandwithSaverContentProducer(byte[] toSend) {
        this.toSend = toSend ;
    }
    

    @Override
    public void writeTo(OutputStream arg0) throws IOException {
        int sent = 0 ;
        while (sent!=toSend.length) {
            BandwithSaver.getInstance(Integer.valueOf(0)).couldSend();
            int current = Math.min(1024, toSend.length-sent);
            arg0.write(toSend,sent,current);
            BandwithSaver.getInstance(Integer.valueOf(0)).addContentSent(current);
            sent+=current;
        }
    }

    private byte[] toSend ;
}
